#include "timer_master_slave.h"

void timer1_timer3_init(void)
{
    TMR_TimerBaseInitType TIM_TimeBaseInitStructure;
	TMR_OCInitType TIM_OCInitStructure;
    GPIO_InitType GPIO_InitStructure;
    TMR_BRKDTInitType TIM_BDTRInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR1,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR3,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOA, ENABLE);     //TIM1_CH1 PA8 master
    RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOB, ENABLE);     //TIM3_CH1 PB4 slave

    GPIO_PinAFConfig(GPIOA, GPIO_PinsSource8, GPIO_AF_2);   
    GPIO_PinAFConfig(GPIOB, GPIO_PinsSource4, GPIO_AF_1);

    GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
    GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
    GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pins = GPIO_Pins_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
    GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
    GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    TMR_Reset(TMR3);
	TIM_TimeBaseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
	TIM_TimeBaseInitStructure.TMR_CounterMode = TMR_CounterDIR_Up;
	TIM_TimeBaseInitStructure.TMR_Period = 100-1;
	TIM_TimeBaseInitStructure.TMR_DIV = 60-1;
	TIM_TimeBaseInitStructure.TMR_RepetitionCounter = 0;
	TMR_TimeBaseInit(TMR3,&TIM_TimeBaseInitStructure);
	
	TMR_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
	TIM_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
	TIM_OCInitStructure.TMR_Pulse = 50;
	TIM_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;
	TMR_OC1Init(TMR3,&TIM_OCInitStructure);

    TMR_SelectSlaveMode(TMR3, TMR_SlaveMode_Trigger);
    TMR_SelectInputTrigger(TMR3, TMR_TRGSEL_ITR0);


    TMR_Reset(TMR1);
	TIM_TimeBaseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
	TIM_TimeBaseInitStructure.TMR_CounterMode = TMR_CounterDIR_Up;
	TIM_TimeBaseInitStructure.TMR_Period = 100-1;
	TIM_TimeBaseInitStructure.TMR_DIV = 120-1;
	TIM_TimeBaseInitStructure.TMR_RepetitionCounter = 0;
	TMR_TimeBaseInit(TMR1,&TIM_TimeBaseInitStructure);
	
	TMR_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
	TIM_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
	TIM_OCInitStructure.TMR_Pulse = 50;
	TIM_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;
	TMR_OC1Init(TMR1,&TIM_OCInitStructure);

    TIM_BDTRInitStructure.TMR_OSIMRState = TMR_OSIMRState_Enable;
    TIM_BDTRInitStructure.TMR_OSIMIState = TMR_OSIMIState_Enable;
    TIM_BDTRInitStructure.TMR_LOCKgrade = TMR_LOCKgrade_OFF;
    TIM_BDTRInitStructure.TMR_DeadTime = 0;
    TIM_BDTRInitStructure.TMR_Break = TMR_Break_Enable;
    TIM_BDTRInitStructure.TMR_BreakPolarity = TMR_BreakPolarity_High;
    TIM_BDTRInitStructure.TMR_AutomaticOutput = TMR_AutomaticOutput_Enable;
    TMR_BRKDTConfig(TMR1, &TIM_BDTRInitStructure);

    TMR_SelectOutputTrigger(TMR1, TMR_TRGOSource_Update);
    TMR_SelectMasterSlaveMode(TMR1, TMR_MasterSlaveMode_Enable);
    TMR_Cmd(TMR1, ENABLE);
    TMR_Cmd(TMR3, ENABLE);

    TMR_CtrlPWMOutputs(TMR1, ENABLE);
    TMR_CtrlPWMOutputs(TMR3, ENABLE);


}

